<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改广告')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-ad-edit" th:object="${shopAd}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">排序：</label>
                <div class="col-sm-8">
                    <input name="orderNumber" th:field="*{orderNumber}" class="form-control" type="number" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">类型：</label>
                <div class="col-sm-8">
                    <select name="mediaType" class="form-control m-b" th:with="type=${@dict.getType('shop_media_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">广告名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">链接：</label>
                <div class="col-sm-8">
                    <input name="link" th:field="*{link}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">广告图片：</label>
                <div class="col-sm-8">
                    <div class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 400px; height: 350px;">
                            <img th:src="*{imageUrl}" th:if="*{imageUrl != null}">
                        </div>
                        <div>
                            <span class="btn btn-white btn-file"><span class="fileinput-new">选择图片</span>
                                <span class="fileinput-exists">更改</span>
                                <input id="img-file" type="file"></span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">广告内容：</label>
                <div class="col-sm-8">
                    <textarea class="form-control" name="content" rows="4">[[*{content}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" th:value="${#dates.format(shopAd.endTime, 'yyyy-MM-dd')}" class="form-control" placeholder="结束时间" type="text" readonly>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">启用状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box">
                        <input type="radio" id="enabled_1" name="enabled" value="1" th:checked="*{enabled == 1}">
                        <label for="enabled_1">启用</label>
                    </div>

                    <div class="radio-box">
                        <input type="radio" id="enabled_0" name="enabled" value="0" th:checked="*{enabled == 0}">
                        <label for="enabled_0">停用</label>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script th:inline="javascript">
        var prefix = ctx + "shop/ad";
        $("#form-ad-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                //$.operate.save(prefix + "/edit", $('#form-ad-edit').serialize());
                uploadFile(prefix + "/edit", $('#form-ad-edit').serializeObject())
            }
        }

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $.fn.serializeObject = function()
        {
            var o = new FormData();
            var formArray = this.serializeArray();
            $.each(formArray, function() {

                o.append(this.name+'', (this.value || ''));
            });
            return o;
        };

        function uploadFile(url, formData) {
            var  file = $('#img-file')[0].files[0];

            if(file != undefined){
                var fileSize = parseInt(file.size / 1024); //KB
                if(fileSize > (1024 * 3)){
                    $.modal.alertWarning("选择的文件大小："+fileSize+"KB，请控制款式图片的大小在100KB - 3M");
                    return;
                }

                formData.append('file', file);
            }

            $.ajax({
                url: url,
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
</html>